<template>
  <div class="app-container">
    <div>
      <!-- 查询 -->
      <div class="filter-container">
        <el-row :gutter="20">
          <el-col :span="18">
            <el-form :inline="true" class="search-form">
              <el-form-item label="等级名称：">
                <el-input
                  v-model="searchForm.name"
                  maxlength="10"
                  show-word-limit
                  class="marginBottom"
                  placeholder="请输入分组名称"
                />
              </el-form-item>
              <el-form-item>
                <el-button size="small" type="primary" icon="el-icon-search" @click="getListWay">查询</el-button>
                <el-button size="small" type="primary" icon="el-icon-refresh" @click="resettingWay">重置</el-button>
              </el-form-item>
            </el-form>
          </el-col>
        </el-row>
      </div>
    </div>
    <el-divider></el-divider>
    <div class="grouping" style="margin-top: 20px">
      <div class="marginBottom">
        <!-- <el-row :gutter="20">
          <el-col>
            <el-button
              type="primary"
              size="small"
              icon="el-icon-circle-plus-outline"
              @click="addGrouping"
            >添加分组</el-button>
          </el-col>
        </el-row> -->
      </div>
      <el-row :gutter="20">
        <el-col>
          <el-table
            ref="groupMultipleSelectionTable"
            v-loading="loading"
            :header-cell-style="{
              'background-color': '#f3f8fe',
              color: '#606266',
              height: '50px',
            }"
            highlight-current-row
            :data="groupList"
            style="width: 100%"
            :row-key="row=>row.id"
            @selection-change="handleSelectionChange"
          >
            <el-table-column prop="id" label="ID" type="selection" width="55" reserve-selection align="center" />
            <el-table-column prop="id" label="ID" align="center" />
            <el-table-column prop="name" label="分组名称" align="center" />
            <!-- <el-table-column fixed="right" width="180" prop="address" label="操作">
              <template slot-scope="scope">
                <el-popconfirm title="这是一段内容确定删除吗？" @confirm="delGradeWay(scope.row)">
                  <el-button slot="reference" type="text" size="small"> 删除
                  </el-button>
                </el-popconfirm>
                <el-button style="margin-left: 10px;" type="text" size="small" @click="putGrade(scope.row)">
                  修改
                </el-button>
              </template>
            </el-table-column> -->
          </el-table></el-col>
      </el-row>
      <el-row class="marginTop" style="text-align: right">
        <el-pagination
          :current-page="searchForm.page"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="searchForm.count"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </el-row>
    </div>

    <el-dialog title="添加分组" :visible.sync="dialogGrouping" width="500px" @close="CloseDialogGrouping">
      <el-form ref="ruleForm" :model="ruleForm" status-icon :rules="rules" label-width="100px" class="demo-ruleForm">
        <el-form-item label="分组名称" prop="name">
          <el-input
            v-model="ruleForm.name"
            type="text"
            placeholder="请输入分组名称"
            onkeyup="value =value.replace(/[^a-zA-Z\u4e00-\u9fa5]/g, '')"
            maxlength="10"
            clearable
            show-word-limit
            autocomplete="off"
          />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="CloseDialogGrouping">取 消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getgroupList, addgrouping, delGroup } from '@/api/user/index'
export default {
  name: 'UserGroupComp',
  data() {
    return {
      loading: false,
      currentPage4: 4,
      dialogGrouping: false,
      groupList: [],
      total: 0,
      ruleForm: {
        name: ''
      },
      searchForm: {
        name: '',
        page: 1,
        count: 10
      },
      rules: {
        name: [
          {
            required: true, validator: (rule, value, callback) => {
              if (!value) {
                return callback(new Error('分组不能为空'))
              }
              callback()
            }, trigger: 'change'
          }
        ]
      },
      multipleSelection: [],
      // 回显避免重复
      // rowSelectFlag: false,
      isRedisplay: false,
      choosedGroupList: [] // 选中的分组列表
    }
  },
  created() {
    this.getListWay()
  },
  methods: {
    getListWay() {
      this.loading = true
      getgroupList(this.searchForm).then((res) => {
        this.loading = false
        if (res.code !== 200) {
          this.$messages.error(res.msg)
          return
        }
        this.groupList = res.data.list
        this.total = res.data.total
        if (this.isRedisplay) {
          this.doRedisplay()
        }
      })
    },
    // 重置
    resettingWay() {
      this.searchForm.name = ''
      this.getListWay()
    },
    // 打开弹框
    addGrouping() {
      this.dialogGrouping = true
    },
    // 关闭弹框
    CloseDialogGrouping() {
      this.$refs.ruleForm.resetFields()
      this.dialogGrouping = false
      this.ruleForm = {
        name: ''
      }
    },
    // 修改
    putGrade(row) {
      this.ruleForm = JSON.parse(JSON.stringify(row))
      this.dialogGrouping = true
    },
    // 提交
    submitForm(formName) {
      var _this = this
      this.$refs[formName].validate((valid) => {
        if (valid) {
          addgrouping(_this.ruleForm).then((res) => {
            if (res.code === 200) {
              this.$messages.success('添加成功')
              this.dialogGrouping = false
              this.getListWay()
            }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 删除
    delGradeWay(row) {
      delGroup({ id: row.id }).then((res) => {
        if (res.code === 200) {
          this.$messages.success('删除成功')
          this.getListWay()
        } else {
          this.$messages.error(res.msg)
        }
      })
    },
    handleSizeChange(val) {
      this.searchForm.count = val
      this.getListWay()
    },
    handleCurrentChange(val) {
      this.searchForm.page = val
      this.getListWay()
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
      this.$emit('usergruopchange', val)
    },
    // 回显指定用户
    redisplayUser(list) {
      this.choosedGroupList = list
      this.isRedisplay = true
      if (this.groupList.length > 0) {
        this.doRedisplay()
      }
    },
    doRedisplay() {
      this.choosedGroupList.forEach(item => {
        const group = this.groupList.find(group => String(group.id) === item.id)
        if (group) {
          this.multipleSelection.push(group)
        }
      })
      // 回显
      this.$nextTick(() => {
        this.multipleSelection.forEach(item => {
          this.$refs.groupMultipleSelectionTable.toggleRowSelection(item, true)
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped></style>
